;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; for daemon mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;only valid in daemon mode ?
;;only for few frame parameters
(setq window-system-default-frame-alist
      '(
        ;; if frame created on x display
        (x
	 (menu-bar-lines . nil)
	 (tool-bar-lines . nil)
	 (vertical-scroll-bars . nil)
	 ;; mouse
	 (mouse-wheel-mode . 1)
	 (mouse-wheel-follow-mouse . t)
	 (mouse-avoidance-mode . 'exile)
	 ;; face
	 ;;(font . "文泉驿等宽微米黑 20")
	 (font . "DejaVu Sans Mono 12")
	 ;;(font . "DejaVu Sans Mono 14")
	 )
	
        ;; if on term
        (nil
	 (menu-bar-lines . 0) (tool-bar-lines . 0)
	 (background-color . "black")
	 ;;(foreground-color . "white")
	 )
	)
      )

;;run after frame created ! (daemon mode run in console mode) (also valid in emacsclient -t ??)
(if (and (fboundp 'daemonp) (daemonp))
    (add-hook 'after-make-frame-functions
			  (lambda (frame)
				(with-selected-frame frame
;;				  (color-theme-oswald)))))
				  (blink-cursor-mode -1)))))

;; (add-hook 'after-make-frame-functions
;; 	  (lambda (new-frame)
;; 	    (select-frame new-frame)
;; 	    (or ibus-mode (ibus-mode-on))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



(ansi-color-for-comint-mode-on) ;; for low version emacs to avoid messy code in shell mode

;; common settings 
(setq inhibit-startup-message t);关闭起动时闪屏
(tool-bar-mode -1)
(menu-bar-mode -1)

;; 让标题栏显示buffer名
;;(setq frame-title-format "emacs@%b")
;;(setq frame-title-format "%b")
;;显示文件路径文件名
(setq frame-title-format
	  '("Emacs[ " (buffer-file-name "%f \]"
										(dired-directory dired-directory "%b \]"))))

;;diplay line number
;;(linum-mode t)
(global-linum-mode t)

;;highlight current line
;;(hl-line-mode t)
;;(global-hl-line-mode t)


;; 设置时间戳，标识出最后一次保存文件的时间	//what is it used for ?
(setq time-stamp-active t)
(setq time-stamp-warn-inactive t)
(setq time-stamp-format "%:y-%02m-%02d %3a %02H:%02M:%02S K.T")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  backup  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq version-control t)    ;;启用版本控制，即可以备份多次
(setq kept-old-versions 2)  ;;备份最原始的版本两次，即第一次编辑前的文档，和第二次编辑前的文档
(setq kept-new-versions 1)  ;;备份最新的版本1次
(setq delete-old-versions t) ;;删掉不属于以上3中版本的版本
(setq backup-directory-alist '(("." . "~/.emacsbackups/"))) ;;设置备份文件的路径
(setq backup-by-copying t)  ;;备份设置方法，直接拷贝
(setq-default make-backup-files t)  ;;不生成临时文件
;;(setq make-backup-files t)
(setq auto-save-default nil)   ;;不生成 #filename# 文件
(setq auto-save-mode nil)      ;;自动保存模式
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;default directory
;; (setq default-directory "~/tmp/")
(setq default-directory "~/")	

;; always end a file with a newline
;;(setq require-final-newline 'query)

;;高亮显示括号匹配
(show-paren-mode t)
(setq show-paren-highlihgt-openparen t)
(setq show-paren-style 'parenthesis)

;;don't display scrollbar 
(scroll-bar-mode -1)

;;光标靠近鼠标指针时，让鼠标指针自动让开
(mouse-avoidance-mode 'animate)

;; cursor not blink (not valid in daemon mode)
;; (blink-cursor-mode nil)

;; 询问时不用 yes or no,用 y/n
;;(fset 'yes-or-no-p 'y-or-n-p)

;; 自动加载被外部程序修改的文件
;;(global-auto-revert-mode t)

;; 设置 TAB 键盘宽度为4
(setq default-tab-width 4)

;; 关闭自动换行功能
(add-hook 'text-mode-hook 'turn-off-auto-fill)

;; 回车自动缩进
(global-set-key "\r" 'newline-and-indent)

;; 支持和外部程序的粘帖, M-w 复制内容到剪切板
;;clipboard-kill-ring-save  : <copy>
;;clipboard-kill-region		: <cut>
;;clipboard-yank			: <paste>
(setq x-select-enable-clipboard t) 


;; default font setting (invalid in daemon mode)
;;(set-default-font "DejaVu Sans Mono-10")
;;(set-default-font "DejaVu Sans Mono-14")
;;(set-default-font "DejaVu Sans Mono-16")
;;(set-default-font "DejaVu Sans-16")
;;(set-default-font "WenQuanYi Micro Hei-20")
;;(set-default-font "WenQuanYi Micro Hei Mono-16")


;;状态栏显示时间
;;(display-time-mode 1)
(setq display-time-day-and-date t)
(setq display-time-24hr-format t)
(setq display-time-format "%Y-%m-%d %H:%M:%S")
(display-time)

;;mode-line height & font
(set-face-attribute 'mode-line nil :box nil 
					:height 100 
					:family "DejaVu Sans Mono" :height 100) 

;;;; copy a line
(defun copy-line (arg)
      "Copy lines (as many as prefix argument) in the kill ring"
      (interactive "p")
      (kill-ring-save (line-beginning-position)
                      (line-beginning-position (+ 1 arg)))
      (message "%d line%s copied" arg (if (= 1 arg) "" "s")))
   ;; optional key binding

(global-set-key "\C-c\C-k" 'copy-line)


;; ;;日历中突出标记节日和生日
;; (setq mark-holidays-in-calendar t)
;; ;;打开calendar自动打开节日和生日列表
;; (setq view-calendar-holidays-initially t)
;; ;; Calendar 中可以看到我们的阴历有中文的天干地支。
;; (setq mark-diary-entries-in-calendar t)
;; (setq appt-issue-message nil)
;; (setq mark-holidays-in-calendar nil)
;; (setq view-calendar-holidays-initially nil)
;; (setq chinese-calendar-celestial-stem
;;       ["甲" "乙" "丙" "丁" "戊" "己" "庚" "辛" "壬" "癸"])
;; (setq chinese-calendar-terrestrial-branch
;;       ["子" "丑" "寅" "卯" "辰" "巳" "午" "未" "申" "酉" "戌" "亥"])



;;----------others----------
;; (setq visible-bell t);关闭出错时的提示声
;; (setq make-backup-files nil);不产生备份文件
;; (setq default-major-mode 'text-mode);一打开就起用 text 模式
;; (global-font-lock-mode t);语法高亮
;; (auto-image-file-mode t);打开图片显示功能
;; (fset 'yes-or-no-p 'y-or-n-p);以 y/n代表 yes/no
;; (column-number-mode t);显示列号
;; (setq mouse-yank-at-point t);支持中键粘贴
;; (transient-mark-mode t);允许临时设置标记
;; (setq x-select-enable-clipboard t);支持emacs和外部程序的粘贴
;; (setq frame-title-format '("" buffer-file-name "@emacs" ));在标题栏显示buffer名称
;; (setq default-fill-column 80);默认显示 80列就换行 


;;scroll
(setq scroll-step 1
      scroll-margin 3
      scroll-conservatively 10000)





;;;;;;;;;;;;  fullscreen for linux & windows ;;;;;;;;;;;;

(defun my-fullscreen()
  (interactive)
  (x-send-client-message
   nil 0 nil "_NET_WM_STATE" 32
   '(2 "_NET_WM_STATE_FULLSCREEN" 0))
)
;;该函数用于最大化,状态值为1说明最大化后不会被还原
;;因为这里有两次最大化 (分别是水平和垂直)
;; (defun my-maximized ()
;;   (interactive)
;;   (x-send-client-message
;;    nil 0 nil "_NET_WM_STATE" 32
;;    '(1 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
;;   (interactive)
;;   (x-send-client-message
;;    nil 0 nil "_NET_WM_STATE" 32
;;    '(1 "_NET_WM_STATE_MAXIMIZED_VERT" 0)))

;; maximized for windows
;; (defun emacs-maximize ()
;;   "Maximize emacs window in windows os"
;;   (interactive)
;;   (w32-send-sys-command 61488))        ; WM_SYSCOMMAND #xf030 maximize
;; (defun emacs-minimize ()
;;   "Minimize emacs window in windows os"
;;   (interactive)
;;   (w32-send-sys-command #xf020))    ; #xf020 minimize
;; (defun emacs-normal ()
;;   "Normal emacs window in windows os"
;;   (interactive)
;;   (w32-send-sys-command #xf120))    ; #xf120 normalimize


;; 启动时最大化
;;(emacs-maximize)
;;(my-fullscreen)




;; shortcuts keys 
(global-set-key [f11] 'my-fullscreen)
;; (global-set-key [f1] 'shell);F1进入Shell
;; (global-set-key [f5] 'gdb);F5调试程序
;; (setq compile-command "make -f Makefile")
(global-set-key [f7] 'do-compile);;F7编译文件
;;(global-set-key [f7] 'compile)
;; (global-set-key [f8] 'other-window);F8窗口间跳转

;;(global-set-key [f10] 'split-window-vertically);F10分割窗口

;;(global-set-key [f11] 'delete-other-windows);;F11 关闭其它窗口
;;(global-set-key [M-return] 'delete-other-windows) ;; (default is C-x 1, no need to change)

(global-set-key [f6] 'shell)
(global-set-key [f10] 'other-window)

;; calculation
(global-set-key (kbd "C-x c") 'quick-calc)

;;goto a line
(global-set-key [(meta g)] 'goto-line)
;;kill this buffer !! c-tab is used !
;;(global-set-key [(ctrl tab)] 'kill-this-buffer)
(global-set-key [C-return] 'kill-this-buffer);C-return关闭当前buffer;

;; ;;auto display line number while goto one line (if default not display line number)
;; (defun my-goto-line ()
;;   "Turn on the LINUM-MODE in the current buffer and invoke function GOTO-LINE and finally turn off LINUM-MODE."
;;   (interactive)
;;   (linum-mode 1)
;;   (unwind-protect (goto-line (string-to-number (read-from-minibuffer "Goto line: ")))
;;     (linum-mode 0)))





;;comment or uncomment region (M-; implement all these command)
;;(global-set-key [(ctrl c) (ctrl c)] 'comment-or-uncomment-region)
;;(global-set-key "\C-c\C-m" 'comment-or-uncomment-region)
;;(global-set-key "\C-x\C-m" 'comment-or-uncomment-region)
;;indent for comment
;;(global-set-key (kbd "C-x /") 'indent-for-comment)

;;对 M-; 的功能稍作修改
;;若选中区域，则注释/反注释整个区域。如果没有选中区域且当前行非空行，并且不是在行尾，注释/反注释当前行。若在行尾(含空行)，在行尾添加注释 
(defun comment-dwim-line (&optional arg) 
"Replacement for the comment-dwim command." 
(interactive "*P") 
(comment-normalize-vars) 
(if (and (not (region-active-p)) (not (looking-at "[ \t]*$"))) 
(comment-or-uncomment-region (line-beginning-position) (line-end-position)) 
(comment-dwim arg)))

(global-set-key (kbd "M-;") 'comment-dwim-line) ;;re-bind key


;;indent-region
(global-set-key (kbd "C-x /") 'indent-region) ;;default is C-M-\


;;取消control+space键设为mark
(global-set-key (kbd "C-SPC") 'nil)


;; % goto the matched paren 
(global-set-key "%" 'match-paren)
          
(defun match-paren (arg)
  "Go to the matching paren if on a paren; otherwise insert %."
  (interactive "p")
  (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
 ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
 (t (self-insert-command (or arg 1)))))


;; plugin load path
(add-to-list 'load-path "~/.emacs.d")

;; use ibus
(require 'ibus)
;; Turn on ibus-mode automatically after loading .emacs (use in daemon mode)
;; (add-hook 'after-init-hook 'ibus-mode-on)
;; in daemon mode
(add-hook 'after-make-frame-functions
	  (lambda (new-frame)
	    (select-frame new-frame)
	    (or ibus-mode (ibus-mode-on))))

;; Use s-SPC to toggle input status
(ibus-define-common-key ?\S-\s nil)
(global-set-key (kbd "s-SPC") 'ibus-toggle)
;; Change cursor color depending on IBus status
;;(setq ibus-cursor-color '("red" "blue" "limegreen"))
;;for archlinux (because it use python3)
(custom-set-variables '(ibus-python-shell-command-name "python2"))


;;;;;;;;;;;;;;;;;;;; rectangle operation (as cua rectangle in cua-mode) ;;;;;;;;;;;;;;;;;;;;
(require 'rect-mark)
(global-set-key (kbd "C-x r C-@") 'rm-set-mark)
(global-set-key (kbd "C-x r C-x C-x") 'rm-exchange-point-and-mark)
(global-set-key (kbd "C-x r C-w") 'rm-kill-region)
(global-set-key (kbd "C-x r M-w") 'rm-kill-ring-save)
(global-set-key (kbd "C-x r C-y") 'yank-rectangle)
(global-set-key   [down-mouse-2]  'rm-mouse-drag-region)    ; mouse drag rect
;;(global-set-key   "\M-I"          'string-insert-rectangle) ; insert string
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;quickly switch to shell
(autoload 'shell-toggle "shell-toggle"
 "Toggles between the *shell* buffer and whatever buffer you are editing."
 t)
(autoload 'shell-toggle-cd "shell-toggle"
 "Pops up a shell-buffer and insert a \"cd <file-dir>\" command." t)
;;(global-set-key (kbd "<C-f12>") 'shell-toggle-cd)	;;this shortcut is alread defined in KDE
(global-set-key (kbd "<C-f6>") 'shell-toggle-cd)

;;auto complete
(add-to-list 'load-path "~/.emacs.d/auto-complete")
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/auto-complete/ac-dict")
(ac-config-default)

;;yasnippet-bundle
;;refer: http://code.google.com/p/yasnippet/
;;(require 'yasnippet-bundle)

;;;refer
;;;;;;yasnippet
;; (add-to-list 'load-path 
;; "~/.emacs.d/plugins/yasnippet-0.6.1c")
;; (require 'yasnippet)
;; (yas/initialize)
;; (yas/load-directory"~/.emacs.d/plugins/yasnippet-0.6.1c/snippets")


;;browse kill ring
(require 'browse-kill-ring)
(require 'browse-kill-ring+)
(global-set-key (kbd "C-c k") 'browse-kill-ring)


;; 高亮显示超出 fill-column 的部分。
;;not useful ?
;;how about c-mode ?
(require 'highlight-beyond-fill-column)
(setq highlight-beyond-fill-column-in-modes
      '("emacs-lisp-mode"))


;; tabbar setting
(require 'tabbar)
(tabbar-mode)
;(global-set-key (kbd "") 'tabbar-backward-group)
;(global-set-key (kbd "") 'tabbar-forward-group)
(global-set-key (kbd "C-`") 'tabbar-backward)
(global-set-key (kbd "C-<tab>") 'tabbar-forward)
(global-set-key (kbd "<M-up>") 'tabbar-backward-group)
(global-set-key (kbd "<M-down>") 'tabbar-forward-group)
;; set tabbar font, height, color 
(set-face-attribute 'tabbar-default-face nil
                    :family "DejaVu Sans Mono"
                    :background "gray80"
                    :foreground "gray30"
                    ;;:height 1.0
					:height 100
                    )
(set-face-attribute 'tabbar-button-face nil 
                    :inherit 'tabbar-default-face
                    :box '(:line-width 1 :color "gray30")
                    )
(set-face-attribute 'tabbar-selected-face nil
                    :inherit 'tabbar-default-face
                    :foreground "DarkGreen"
                    :background "LightGoldenrod"
                    :box '(:line-width 2 :color "DarkGoldenrod")
                    ;; :overline "black"
                    ;; :underline "black"
                    :weight 'bold
                    )
(set-face-attribute 'tabbar-unselected-face nil
                    :inherit 'tabbar-default-face
                    :box '(:line-width 2 :color "gray70")
                    )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; the tabbar-buffer-groups in tabbar.el is modified !
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (defun kid-tabbar-buffer-groups-function (buffer)
;;   "Return the list of group names BUFFER belongs to.
;; Return only one group for each buffer."
;;   (with-current-buffer (get-buffer buffer)
;;     (cond
;;      ((or (get-buffer-process (current-buffer))
;;           (memq major-mode
;;                 '(comint-mode compilation-mode)))
;;       '("Process"))
;;      ((or (member (buffer-name)
;;                '("*scratch*" "*Messages*"))
;;        (memq major-mode
;;              '(help-mode apropos-mode Info-mode Man-mode
;;                completion-list-mode)))
;;       '("Common"))
;;      ((eq major-mode 'dired-mode)
;;       '("Dired"))
;;      ((memq major-mode
;;             '(rmail-mode
;;               rmail-edit-mode vm-summary-mode vm-mode mail-mode
;;               mh-letter-mode mh-show-mode mh-folder-mode
;;               gnus-summary-mode message-mode gnus-group-mode
;;               gnus-article-mode score-mode gnus-browse-killed-mode))
;;       '("Mail"))
;;      ((memq major-mode
;;          '(c-mode c++-mode ruby-mode python-mode emacs-lisp-mode))
;;       '("Develop"))
;;      ((memq major-mode
;;          '(muse-mode LaTeX-mode org-mode text-mode))
;;       '("Document"))
;;      (t
;;       (list
;;        "default"  ;; no-grouping
;;        (if (and (stringp mode-name) (string-match "[^ ]" mode-name))
;;            mode-name
;;          (symbol-name major-mode)))))))
;; (setq tabbar-buffer-groups-function 'kid-tabbar-buffer-groups-function)



;; ibuffer ,not very useful 
;;(require 'ibuffer)
;;(global-set-key (kbd "C-x C-y") 'ibuffer)

;; kill some buffers
;; C-x C-b , then
;; d or C-d		make a delete mark
;; s			save mark
;; u			cancel a mark
;; x			perform


;;making buffer names unique
(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)


;;kill all opened buffers & killall other buffers(except current, info, message buffers)
(defun kill-all-buffers ()
  (interactive)
  (mapc 'kill-buffer (buffer-list)))

(defun kill-other-buffers ()
    "Kill all other buffers."
    (interactive)
    (mapc 'kill-buffer 
          (delq (current-buffer) 
                (remove-if-not 'buffer-file-name (buffer-list)))))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  save sessions & desktop  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'session)
(add-hook 'after-init-hook 'session-initialize)

(require 'desktop)
;; some settings
;; (setq history-length 250)
;; (setq desktop-globals-to-save '(desktop-missing-file-warning))

;; ;; buffers which should not be saved
;; (setq desktop-buffers-not-to-save
;; 	  (concat "\\("
;; 			  "^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS"
;; 			  "\\|\\.emacs.*\\|\\.diary\\|\\.newsrc-dribble\\|\\.bbdb"
;; 			  "\\)$"))
;; (add-to-list 'desktop-modes-not-to-save 'dired-mode)
;; (add-to-list 'desktop-modes-not-to-save 'Info-mode)
;; (add-to-list 'desktop-modes-not-to-save 'info-lookup-mode)
;; (add-to-list 'desktop-modes-not-to-save 'fundamental-mode)

;; use only one desktop
(setq desktop-path '("~/.emacs.d/"))
(setq desktop-dirname "~/.emacs.d/")
(setq desktop-base-file-name ".emacs-desktop")

;;;;;;; desktop functions ;;;;;;
(defun saved-session ()
  (file-exists-p (concat desktop-dirname "/" desktop-base-file-name)))

;; use session-restore to restore the desktop manually
(defun session-restore ()
  "Restore a saved emacs session."
  (interactive)
  (if (saved-session)
      (desktop-read)
    (message "No desktop found.")))

;; use session-save to save the desktop manually
(defun session-save ()
  "Save an emacs session."
  (interactive)
  (if (saved-session)
      (if (y-or-n-p "Overwrite existing desktop? ")
	  (desktop-save-in-desktop-dir)
	(message "Session not saved."))
  (desktop-save-in-desktop-dir)))

(defun my-desktop-save ()
  (noninteractive)
  ;; Don't call desktop-save-in-desktop-dir, as it prints a message.
  (if (eq (desktop-owner) (emacs-pid))
	  (desktop-save desktop-dirname)))
;;;;;;;;;;;;;;;;

;; automatic save/restore the desktop when exit/start-up (but it will ask user to save when quit ???)
(desktop-save-mode 1)
;; ;; or ask user whether to save desktop at exit
;; (add-hook 'auto-save-hook 'my-desktop-save)
;; ;; or ask user whether to restore desktop at start-up
;; (add-hook 'after-init-hook
;; 	  '(lambda ()
;; 	     (if (saved-session)
;; 		 (if (y-or-n-p "Restore desktop? ")
;; 		     (session-restore)))))

;; ;; remove desktop after it's been read
;; (add-hook 'desktop-after-read-hook
;; 	  '(lambda ()
;; 	     ;; desktop-remove clears desktop-dirname
;; 	     (setq desktop-dirname-tmp desktop-dirname)
;; 	     (desktop-remove)
;; 	     (setq desktop-dirname desktop-dirname-tmp)))

;; If you are using desktop-mode and your emacs crashes (or more likely, your system crashes),
;; then your desktop file will not be released, and emacs will bother you about using it next time you start it up.
;; thus, emacs will be blocked in daemon mode, like this:
;; "Warning: desktop file appears to be in use by PID 19433."
;; "Using it may cause conflicts.  Use it anyway? (y or n)"
;; avoid this, override the lock on a desktop file if the indicated process is not still running

;;override the lock on a desktop file if the indicated process is not still running
(defun emacs-process-p (pid)
  "If pid is the process ID of an emacs process, return t, else nil.
Also returns nil if pid is nil."
  (when pid
    (let ((attributes (process-attributes pid)) (cmd))
      (dolist (attr attributes)
        (if (string= "comm" (car attr))
            (setq cmd (cdr attr))))
      (if (and cmd (or (string= "emacs" cmd) (string= "emacs.exe" cmd))) t))))

(defadvice desktop-owner (after pry-from-cold-dead-hands activate)
  "Don't allow dead emacsen to own the desktop file."
  (when (not (emacs-process-p ad-return-value))
    (setq ad-return-value nil)))

;; or : (not effective)
;; noninteractive while in daemon mode, otherwise it will blocked: 
;; (defadvice desktop-restore-file-buffer
;;   (around my-desktop-restore-file-buffer-advice)
;;   "Be non-interactive while starting a daemon."
;;   (if (and (daemonp)
;;            (not server-process))
;;       (let ((noninteractive t))
;;         ad-do-it)
;;     ad-do-it))
;; (ad-activate 'desktop-restore-file-buffer)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; color theme ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'color-theme)
(color-theme-initialize)
;;(color-theme-oswald)
;;(color-theme-jonadabian-slate)
;;(color-theme-euphoria)
;;gray30,lethe,marine,retro orange,,Taming Mr Arneson,Taylor,TTY Dark
;;(color-theme-classic)
;;(color-theme-gnome2)
;;(color-theme-arjen)				;;red, beautiful
;;(color-theme-bharadwaj-slate)
;;(color-theme-deep-blue)
;;(color-theme-calm-forest)

;;(color-theme-late-night)
;;(color-theme-midnight)

;; ;;decide under X or console
;; (if window-system
;;  	(color-theme-billw)			;;under X 
;;   (color-theme-arjen))			;;else in console (so daemon mode)

;; (color-theme-billw)
;; (color-theme-charcoal-black)
;; (color-theme-goldenrod)
;; (color-theme-comidia)
;; (color-theme-digital-ofs1)	;; background is light color

(set-background-color "black")
(set-foreground-color "orchid")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;export buffer or region to html
(require 'htmlize)


;; for graphviz (dot mode)
(load-file "~/.emacs.d/graphviz-dot-mode.el")


;;for verilog

;;1. verilog-mode (within emacs)

;;2. vlog-mode
(add-to-list 'load-path "~/.emacs.d/vlog-mode-1.6")
(require 'vlog-mode)

(add-to-list 'auto-mode-alist '("\\.v\\'" . vlog-mode))
(add-to-list 'auto-mode-alist '("\\.vl\\'" . vlog-mode))

(vlog-mode-enable-v2k)

(setq vlog-mode-highlight-all-uppercase-words t)

(setq vlog-align-mod-inst-stop-list '(28 52))

(setq vlog-indent-level-beh                2
      vlog-indent-level-block              0
      vlog-indent-level-block-beh          0
      vlog-indent-level-block-inside       2
      vlog-indent-level-case-inside        4
      vlog-indent-level-case-branch-inside 2
      vlog-indent-level-cond               2
      vlog-indent-level-default            2
      vlog-indent-level-port-list          4)

(setq vlog-mode-keywordset-docs
      (append vlog-mode-keywordset-docs
              (list "Note:" "NOTE:" "note:")))
(vlog-mode-make-keywords)

(setq vlog-skel-header-string "\
//^File Header -----------------------------------------< Powered By Emacs >--
// Copyright (C) %<time %Y> %<company>
// All rights reserved
// ---------------------------------------------------------------------------
// $Id$
// FILE NAME   : %<filename>
// MODULE NAME : %<modulename>
// AUTHOR      : %<author>
// ---------------------------------------------------------------------------
// [RELEASE HISTORY]                               Last Modified : %<time %Y-%m-%d>
// VERSION  DATE        AUTHOR      DESCRIPTION
// 1.0%<align>%<time %y-%m-%d>%<align>%<author>%<align>Original
// ---------------------------------------------------------------------------
// [DESCRIPTION]
// %<_>
// ---------------------------------------------------------------------------
// [SUBMODULE LIST]
// [ N o n e ]
// ---------------------------------------------------------------------------
// [PARAMETERS]
// PARAM_NAME   RANGE               DEFAULT         DESCRIPTION
// ----------   -----               -------         -----------
// [ N o n e ]
//$File Header -----------------------------------------< Powered By Emacs >--\n")

(setq vlog-skel-user-name    "sk.chang"
      vlog-skel-company-name "GNU")








	  




	  

;;(add-to-list 'load-path "/usr/share/emacs/site-lisp/w3m")



;; (add-to-list 'load-path "~/.emacs.d/emacs-w3m")
;; (require 'w3m)
;; (require 'w3m-lnum)

;; (require 'util)

;; (defvar w3m-buffer-name-prefix "*w3m" "Name prefix of w3m buffer")
;; (defvar w3m-buffer-name (concat w3m-buffer-name-prefix "*") "Name of w3m buffer")
;; (defvar w3m-bookmark-buffer-name (concat w3m-buffer-name-prefix "-bookmark*") "Name of w3m buffer")
;; ;;(defvar w3m-dir (concat my-emacs-lisps-path "~/.emacs.d/emacs-w3m/") "Dir of w3m.")
;; (defconst w3m-dir "~/.emacs.d/emacs-w3m/")

;; (setq w3m-command-arguments '("-cookie" "-F"))
;; (setq w3m-use-cookies t)
;; (setq w3m-icon-directory (concat w3m-dir "icons"))
;; (setq w3m-use-mule-ucs t)
;; (setq w3m-home-page "http://www.google.com")
;; (setq w3m-default-display-inline-images t)
 
;; (defun w3m-settings ()
;;   (make-local-variable 'hl-line-face)
;;   (setq hl-line-face 'hl-line-nonunderline-face)
;;   (setq hl-line-overlay nil))
;;   ;;(color-theme-adjust-hl-line-face))
 
;; (add-hook 'w3m-mode-hook 'w3m-settings)
 
;; (defun w3m-save-current-buffer ()
;;   "Save current w3m buffer."
;;   (interactive)
;;   (save-excursion
;;     (mark-whole-buffer)
;;     (call-interactively 'copy-region-as-kill-nomark))
;;   (with-temp-buffer
;;     (call-interactively 'yank)
;;     (call-interactively 'write-file)))
 
;; (defun w3m-print-current-url ()
;;   "Display current url."
;;   (interactive)
;;   (w3m-message "%s" (w3m-url-readable-string w3m-current-url)))
 
;; (defun w3m-copy-current-url ()
;;   "Display the current url in the echo area and put it into `kill-ring'."
;;   (interactive)
;;   (when w3m-current-url
;;     (let ((deactivate-mark nil))
;;       (kill-new w3m-current-url)
;;       (w3m-print-current-url))))
 
;; (defun view-w3m-bookmark ()
;;   "View w3m bokmark."
;;   (interactive)
;;   (let ((buffer (get-buffer w3m-bookmark-buffer-name)))
;;     (if buffer
;;         (switch-to-buffer buffer)
;;       (with-current-buffer (get-buffer-create w3m-bookmark-buffer-name)
;;         (w3m-mode)
;;         (w3m-bookmark-view)))))
 
;; (defun switch-to-w3m ()
;;   "Switch to *w3m* buffer."
;;   (interactive)
;;   (let ((buffer (get-buffer w3m-buffer-name)))
;;     (if buffer
;;         (switch-to-buffer buffer)
;;       (message "Could not found w3m buffer."))))
 
;; (defun w3m-browse-current-buffer ()
;;   "Use w3m browser current buffer."
;;   (interactive)
;;   (w3m-browse-buffer))
 
;; (defun w3m-browse-buffer (&optional buffer)
;;   "Use w3m browser buffer BUFFER."
;;   (interactive "bBuffer to browse use w3m: ")
;;   (unless buffer (setq buffer (current-buffer)))
;;   (let* ((file (buffer-file-name buffer))
;;          (name (buffer-name buffer)))
;;     (if file
;;         (w3m-goto-url-new-session file)
;;       (with-current-buffer buffer
;;         (save-excursion
;;           (mark-whole-buffer)
;;           (call-interactively 'copy-region-as-kill-nomark)))
;;       (let* ((new-name
;;               (concat
;;                w3m-buffer-name-prefix
;;                "-"
;;                (if (string= "*" (substring name 0 1))
;;                    (substring name 1)
;;                  (concat name "*"))))
;;              (new-buffer (get-buffer-create new-name)))
;;         (switch-to-buffer new-buffer)
;;         (call-interactively 'yank)
;;         (w3m-buffer)
;;         (w3m-mode)
;;         (setq w3m-current-title (buffer-name))))))
 
;; ;; fix small bug about of `w3m-auto-show'
;; ;; see my-blog/emacs/w3m-auto-show-bug.htm
;; (defun w3m-auto-show ()
;;   "Scroll horizontally so that the point is visible."
;;   (when (and truncate-lines
;;              w3m-auto-show
;;              (not w3m-horizontal-scroll-done)
;;              (not (and (eq last-command this-command)
;;                        (or (eq (point) (point-min))
;;                            (eq (point) (point-max)))))
;;              (or (memq this-command '(beginning-of-buffer end-of-buffer))
;;                  (string-match "\\`i?search-"
;;                                (if (symbolp this-command) (symbol-name this-command) ""))
;;                  (and (markerp (nth 1 w3m-current-position))
;;                       (markerp (nth 2 w3m-current-position))
;;                       (>= (point)
;;                           (marker-position (nth 1 w3m-current-position)))
;;                       (<= (point)
;;                           (marker-position (nth 2 w3m-current-position))))))
;;     (w3m-horizontal-on-screen))
;;   (setq w3m-horizontal-scroll-done nil))
 
;; (defun w3m-link-numbering (&rest args)
;;   "Make overlays that display link numbers."
;;   (when w3m-link-numbering-mode
;;     (save-excursion
;;       (goto-char (point-min))
;;       (let ((i 0)
;;             overlay num)
;;         (catch 'already-numbered
;;           (while (w3m-goto-next-anchor)
;;             (when (get-char-property (point) 'w3m-link-numbering-overlay)
;;               (throw 'already-numbered nil))
;;             (setq overlay (make-overlay (point) (1+ (point)))
;;                   num (format "[%d]" (incf i)))
;;             (w3m-static-if (featurep 'xemacs)
;;                 (progn
;;                   (overlay-put overlay 'before-string num)
;;                   (set-glyph-face (extent-begin-glyph overlay)
;;                                   'w3m-link-numbering))
;;               (w3m-add-face-property 0 (length num) 'w3m-link-numbering num)
;;               (overlay-put overlay 'before-string num)
;;               (overlay-put overlay 'evaporate t))
;;             (overlay-put overlay 'w3m-link-numbering-overlay i)))))))
 
;; (apply-define-key
;;  global-map
;;  `(("M-M"     w3m-goto-url-new-session)
;;    ("C-x M-B" view-w3m-bookmark)
;;    ("C-x M-m" switch-to-w3m)))
 
;; (apply-define-key
;;  w3m-mode-map
;;   `(("<backtab>" w3m-previous-anchor)
;;     ("n"         w3m-next-anchor)
;;     ("p"         w3m-previous-anchor)
;;     ("w"         w3m-next-form)
;;     ("b"         w3m-previous-form)
;;     ("f"         w3m-go-to-linknum)
;;     ("M-n"       w3m-next-buffer)
;;     ("M-p"       w3m-previous-buffer)
;;     ("C-k"       kill-this-buffer)
;;     ("C-k"       w3m-delete-buffer)
;;     ("C-c 1"     w3m-delete-other-buffers)
;;     ("1"         delete-other-windows)
;;     ("C-x C-s"   w3m-save-current-buffer-sb)
;;     ("P"         w3m-print-current-url)
;;     ("U"         w3m-print-this-url)
;;     ("c"         w3m-copy-current-url)
;;     ("g"         w3m-goto-url-new-session)
;;     ("G"         w3m-goto-url)
;;     ("d"         w3m-download-this-url-sb)
;;     ("M-d"       w3m-download-sb)
;;     ("s"         w3m-search)
;;     ("S"         w3m-history)
;;     ("u"         View-scroll-page-backward)
;;     ("J"         roll-down)
;;     ("K"         roll-up)
;;     ("o"         other-window)
;;     ("m"         w3m-view-this-url-new-session)
;;     ("C-h"       w3m-view-previous-page)
;;     ("F"         w3m-view-next-page)
;;     ("C-;"       w3m-view-next-page)
;;     ("r"         w3m-reload-this-page)
;;     ("v"         w3m-bookmark-view-new-session)
;;     ("M-e"       w3m-bookmark-edit)
;;     ("'"         switch-to-other-buffer)))

;; (require 'w3m)
;; (setq w3m-use-mule-ucs t)
;; (setq w3m-use-toolbar t)
;; (setq w3m-use-cookies t)
;; (setq w3m-display-inline-image t)
;; ;;(setq w3m-default-display-inline-images t) 
;; (w3m-toggle-inline-images)

;; (setq w3m-bookmark-file-coding-system 'chinese-iso-8bit)
;; (setq w3m-coding-system 'chinese-iso-8bit)
;; (setq w3m-default-coding-system 'chinese-iso-8bit)
;; (setq w3m-file-coding-system 'chinese-iso-8bit)
;; (setq w3m-file-name-coding-system 'chinese-iso-8bit)
;; (setq w3m-terminal-coding-system 'chinese-iso-8bit)
;; (setq w3m-input-coding-system 'chinese-iso-8bit)
;; (setq w3m-output-coding-system 'chinese-iso-8bit)
;; (setq w3m-tab-width 8)

;; (setq w3m-home-page "http://www.google.com")
;; (setq w3m-view-this-url-new-session-in-background t)
;; ;(require 'mime-w3m)  
;; (add-hook 'w3m-fontify-after-hook 'remove-w3m-output-garbages)
;; (defun remove-w3m-output-garbages ()
;;       (interactive)
;;       (let ((buffer-read-only))
;;         (setf (point) (point-min))
;;         (while (re-search-forward "[\200-\240]" nil t)
;;           (replace-match " "))
;;         (set-buffer-multibyte t))

;;       (set-buffer-modified-p nil))



 ;; nc file browser(ms-dog)
 ;;(require 'nc)



 ;; others to be tested 
 ;;(require 'tree-widget)




 ;; 参考配置
 ;; 再贴一下完整的配置，基本上是参照其它网友提供的改的： 

 ;; 代码:
 ;; ;插件路径
 ;; (add-to-list 'load-path "/home/share/myemacs")
 ;; (add-to-list 'load-path "/home/share/myemacs/ecb")
 ;; (add-to-list 'load-path "/home/share/myemacs/xcscope")
 ;; (add-to-list 'load-path "/home/share/myemacs/cedet/common")

 ;; ;; some template 
 ;; ;; (kill-this-buffer);关闭customize窗口






 ;; ;; ;;鼠标滚轮，默认的滚动太快，这里改为3行
 ;; ;; (defun up-slightly () (interactive) (scroll-up 3))
 ;; ;; (defun down-slightly () (interactive) (scroll-down 3))
 ;; ;; (global-set-key [mouse-4] 'down-slightly)
 ;; ;; (global-set-key [mouse-5] 'up-slightly)


;;非交互式编译
(defun do-compile ()
  "Save buffers and start compile"
  (interactive)
  (save-some-buffers t)
  (setq compilation-read-command nil)
  (compile compile-command)
  (setq compilation-read-command t))


 ;; ;shell,gdb退出后，自动关闭该buffer
 ;; (add-hook 'shell-mode-hook 'mode-hook-func)
 ;; (add-hook 'gdb-mode-hook 'mode-hook-func)
 ;; (defun mode-hook-func  ()
 ;;   (set-process-sentinel (get-buffer-process (current-buffer))
 ;;          #'kill-buffer-on-exit))
 ;; (defun kill-buffer-on-exit (process state)
 ;;   (message "%s" state)
 ;;   (if (or
 ;;        (string-match "exited abnormally with code.*" state)
 ;;        (string-match "finished" state))
 ;;       (kill-buffer (current-buffer))))



 ;; ;加入xcscope插件
 ;; (require 'xcscope)


 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;C/C++设定;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 ;;; refer 1:
;; ;;;;----------------- Programming ------------------------
;; ;; CEDET: http://cedet.sourceforge.net/
;; (add-to-list 'load-path "~/etc/lisp/cedet/common")
;; (require 'cedet)
;; (semantic-load-enable-excessive-code-helpers)
;; (setq semanticdb-default-save-directory "~/etc/emacs/") ; semanticdb默认路经

;; ;; 设置编译命令为:make
;; (setq compile-command "make")

;; ;; C/C++语言风格
;; (defun wx-c-mode-hook()
;; (setq tab-width 4 indent-tabs-mode nil)
;; (c-set-style "stroustrup")
;; ; 关闭自动换行
;; (c-toggle-auto-newline -1)
;; ; 贪心删除
;; (c-toggle-auto-hungry-state 1)
;; ; ctrl+`: 代码折叠
;; (define-key c-mode-base-map [(control \`)] 'hs-toggle-hiding)
;; ; 换行自动递进
;; (define-key c-mode-base-map [(return)] 'newline-and-indent)
;; ; F7:编译
;; (define-key c-mode-base-map [(f7)] 'compile)

;; )
;; (add-hook 'c-mode-hook 'wx-c-mode-hook)
;; (add-hook 'c++-mode-hook 'wx-c-mode-hook)


;;;;---------------- Personal Info. ----------------------
(setq user-full-name "sk.chang")
(setq user-mail-address "sk.chang0722@gmail.com")




 ;;refer 2:
 ;;
 ;;;;缩进策略
 ;; (defun my-indent-or-complete ()
 ;;   (interactive)
 ;;   (if (looking-at "\\>")
 ;;       (hippie-expand nil)
 ;;     (indent-for-tab-command)))
 ;; ;补全优先级
 ;; (autoload 'senator-try-expand-semantic "senator")
 ;; (setq hippie-expand-try-functions-list
 ;;       '(
 ;;    senator-try-expand-sematic
 ;;    try-expand-dabbrev
 ;;    try-expand-dabbrev-visible
 ;;    try-expand-dabbrev-all-buffers
 ;;    try-expand-dabbrev-from-kill
 ;;    try-complete-file-name-partially
 ;;    try-complete-file-name
 ;;    try-expand-all-abbrevs
 ;;    try-expand-list
 ;;    try-expand-line
 ;;    try-complete-lisp-symbol-partially
 ;;    try-complete-lisp-symbol))
 ;; ;;;; CC-mode配置  http://cc-mode.sourceforge.net/
 ;; (require 'cc-mode)
 ;; (c-set-offset 'inline-open 0)
 ;; (c-set-offset 'friend '-)
 ;; (c-set-offset 'substatement-open 0)
 ;; ;;;;根据后缀判断所用的mode
 ;; ;;;;注意：我在这里把.h关联到了c++-mode
 ;; (setq auto-mode-alist
 ;;       (append '(("\\.h$" . c++-mode)) auto-mode-alist))
 ;; ;;;;我的C语言编辑策略
(defun my-c-mode-common-hook()
   (setq default-tab-width 4 indent-tabs-mode nil)
   (setq tab-width 2 indent-tabs-mode nil)
   (setq c-basic-offset 2)

   ;;; hungry-delete and auto-newline
   (c-toggle-auto-hungry-state 1)
   (c-toggle-auto-newline -1)

   ;;按键定义
   ;(define-key c-mode-base-map [(control \`)] 'hs-toggle-hiding)
   ;;(define-key c-mode-base-map [(return)] 'newline-and-indent)
   ;;(define-key c-mode-base-map [(f7)] 'do-compile)
   ;(define-key c-mode-base-map [(f8)] 'ff-get-other-file)
   ;;(define-key c-mode-base-map [(meta \`)] 'c-indent-command)

   ;(define-key c-mode-base-map [(tab)] 'hippie-expand)
   ;;(define-key c-mode-base-map [(tab)] 'my-indent-or-complete)
   (define-key c-mode-base-map [(meta ?/)] 'semantic-ia-complete-symbol-menu)
   ;;预处理设置
   (setq c-macro-shrink-window-flag t)
   (setq c-macro-preprocessor "cpp")
     (setq c-macro-cppflags " ")
   (setq c-macro-prompt-flag t)
   (setq hs-minor-mode t)
   (setq abbrev-mode t))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
 ;; ;;;;我的C++语言编辑策略
 ;; (defun my-c++-mode-hook()
 ;;   (setq default-tab-width 4 indent-tabs-mode nil)
 ;;   (setq tab-width 4 indent-tabs-mode nil)
 ;;   (setq c-basic-offset 4)
 ;;   ;;(define-key c++-mode-map [f3] 'replace-regexp)
 ;;   (c-set-style "stroustrup"))
 ;;   (add-hook 'c++-mode-hook 'my-c++-mode-hook)


 ;; ;加入cedet插件
 ;; (add-hook 'texinfo-mode-hook (lambda () (require 'sb-texinfo)))
 ;; (require 'cedet)
 ;; (semantic-load-enable-code-helpers)
 ;; (autoload 'speedbar-frame-mode "speedbar" "Popup a speedbar frame" t)
 ;; (autoload 'speedbar-get-focus "speedbar" "Jump to speedbar frame" t)
 ;; (define-key-after (lookup-key global-map [menu-bar tools])
 ;;   [speedbar]
 ;;   '("Speedbar" .
 ;;     speedbar-frame-mode)
 ;;   [calendar])
 ;; (global-set-key [f4] 'speedbar);F4打开/关闭speedbar
 ;; ;;;;semantic /usr/include      
 ;; (setq semanticdb-search-system-databases t)
 ;; (add-hook 'c-mode-common-hook
 ;;           (lambda ()
 ;;             (setq semanticdb-project-system-databases
 ;;                   (list (semanticdb-create-database
 ;;           semanticdb-new-database-class
 ;;           "/usr/include")))))
 ;; ;project root path
 ;; (setq semanticdb-project-roots 
 ;;       (list
 ;;        (expand-file-name "~/devel")))


 ;; ;加入ecb插件
 ;; (require 'ecb)
 ;; ;ecb设置
 ;; (require 'ecb-autoloads)
 ;; (setq ecb-auto-activate t
 ;;       ecb-tip-of-the-day nil
 ;;       inhibit-startup-message t
 ;;       ecb-auto-compatibility-check nil
 ;;       ecb-version-check nil) 



 ;; (custom-set-variables
 ;;   ;; custom-set-variables was added by Custom.
 ;;   ;; If you edit it by hand, you could mess it up, so be careful.
 ;;   ;; Your init file should contain only one such instance.
 ;;   ;; If there is more than one, they won't work right.
 ;; '(ecb-layout-window-sizes (quote (("left8" (0.20967741935483872 . 0.27586206896551724) (0.20967741935483872 . 0.2413793103448276) (0.20967741935483872 . 0.27586206896551724) (0.20967741935483872 . 0.1724137931034483)))))
 ;; '(ecb-primary-secondary-mouse-buttons (quote mouse-1--C-mouse-1)))
 ;; (custom-set-faces
 ;;   ;; custom-set-faces was added by Custom.
 ;;   ;; If you edit it by hand, you could mess it up, so be careful.
 ;;   ;; Your init file should contain only one such instance.
 ;;   ;; If there is more than one, they won't work right.
 ;; )


 ;; ;scroll other window
 ;; (global-set-key (kbd "C-c C-v") 'scroll-other-window)
 ;; (global-set-key (kbd "C-c C-b") 'scroll-other-window-down)




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  CEDET  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; (load-file "~/.emacs.d/cedet/common/cedet.el")
;; (global-ede-mode 1)                      ; Enable the Project management system
;; (semantic-load-enable-code-helpers)      ; Enable prototype help and smart completion 
;; (global-srecode-minor-mode 1)            ; Enable template insertion menu


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; refer : http://emacser.com/cedet.htm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(load-file "~/.emacs.d/cedet-1.1/common/cedet.el")
;;(global-ede-mode 1)                      ; Enable the Project management system
;;(semantic-load-enable-code-helpers)      ; Enable prototype help and smart completion 
;;(global-srecode-minor-mode 1)            ; Enable template insertion menu

;; (semantic-load-enable-minimum-features)
(semantic-load-enable-code-helpers)
;;(semantic-load-enable-guady-code-helpers)
;;(semantic-load-enable-gaudy-code-helpers)		;; display function name in the top of buffer
(which-func-mode t)
(global-semantic-decoration-mode 1)
(global-semantic-highlight-func-mode 1)
;; (semantic-load-enable-excessive-code-helpers)
(semantic-load-enable-semantic-debugging-helpers)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; set include path  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (setq semanticdb-project-roots (list (expand-file-name "/")))
(defconst cedet-user-include-dirs
  (list ".." "../include" "../inc" "../common" "../public"
        "../.." "../../include" "../../inc" "../../common" "../../public"))
(defconst cedet-win32-include-dirs
  (list "C:/MinGW/include"
        "C:/MinGW/include/c++/3.4.5"
        "C:/MinGW/include/c++/3.4.5/mingw32"
        "C:/MinGW/include/c++/3.4.5/backward"
        "C:/MinGW/lib/gcc/mingw32/3.4.5/include"
        "C:/Program Files/Microsoft Visual Studio/VC98/MFC/Include"))
(require 'semantic-c nil 'noerror)
(let ((include-dirs cedet-user-include-dirs))
  (when (eq system-type 'windows-nt)
    (setq include-dirs (append include-dirs cedet-win32-include-dirs)))
  (mapc (lambda (dir)
          (semantic-add-system-include dir 'c++-mode)
          (semantic-add-system-include dir 'c-mode))
        include-dirs))


(global-set-key [f12] 'semantic-ia-fast-jump)
;; shortcut [C-x B] [RET]
(global-set-key [S-f12]
                (lambda ()
                  (interactive)
                  (if (ring-empty-p (oref semantic-mru-bookmark-ring ring))
                      (error "Semantic Bookmark ring is currently empty"))
                  (let* ((ring (oref semantic-mru-bookmark-ring ring))
                         (alist (semantic-mrub-ring-to-assoc-list ring))
                         (first (cdr (car alist))))
                    (if (semantic-equivalent-tag-p (oref first tag)
                                                   (semantic-current-tag))
                        (setq first (cdr (car (cdr alist)))))
                    (semantic-mrub-switch-tags first))))


(define-key c-mode-base-map [M-S-f12] 'semantic-analyze-proto-impl-toggle)

;; code auto-complete ;;
(define-key c-mode-base-map (kbd "M-n") 'semantic-ia-complete-symbol-menu)		;; only valid in GUI
;;;;;;;; other method for auto-complete ;;;;;;;;
;; senator-complete-symbol
;; senator-completion-menu-popup
;; semantic-ia-complete-symbol

(global-semantic-idle-completions-mode 1)		;;auto complete while idle


;;;; EDE
;;(global-ede-mode t)

;;;; switch between h/cpp
(require 'eassist nil 'noerror)
(setq eassist-header-switches
      '(("h" . ("cpp" "cxx" "c++" "CC" "cc" "C" "c" "mm" "m"))
        ("hh" . ("cc" "CC" "cpp" "cxx" "c++" "C"))
        ("hpp" . ("cpp" "cxx" "c++" "cc" "CC" "C"))
        ("hxx" . ("cxx" "cpp" "c++" "cc" "CC" "C"))
        ("h++" . ("c++" "cpp" "cxx" "cc" "CC" "C"))
        ("H" . ("C" "CC" "cc" "cpp" "cxx" "c++" "mm" "m"))
        ("HH" . ("CC" "cc" "C" "cpp" "cxx" "c++"))
        ("cpp" . ("hpp" "hxx" "h++" "HH" "hh" "H" "h"))
        ("cxx" . ("hxx" "hpp" "h++" "HH" "hh" "H" "h"))
        ("c++" . ("h++" "hpp" "hxx" "HH" "hh" "H" "h"))
        ("CC" . ("HH" "hh" "hpp" "hxx" "h++" "H" "h"))
        ("cc" . ("hh" "HH" "hpp" "hxx" "h++" "H" "h"))
        ("C" . ("hpp" "hxx" "h++" "HH" "hh" "H" "h"))
        ("c" . ("h"))
        ("m" . ("h"))
        ("mm" . ("h"))))

;; 通过cpp找头文件时它也会上配置好的INCLUDE路径中去查找，不过如果通过头文件找cpp文件，好像只能找和头文件所在的同一目录了
;; (define-key c-mode-base-map [M-f12] 'eassist-switch-h-cpp)
(define-key c-mode-base-map (kbd "M-p") 'eassist-switch-h-cpp)			;;"M-o" to open the .h file from a #include line

;;;; code folding (after semantic parse finished) (code not align while in terminal)
(require 'semantic-tag-folding nil 'noerror)
(global-semantic-tag-folding-mode 1)
;; (define-key semantic-tag-folding-mode-map (kbd "C-c , -") 'semantic-tag-folding-fold-block)
;; (define-key semantic-tag-folding-mode-map (kbd "C-c , +") 'semantic-tag-folding-show-block)
(define-key semantic-tag-folding-mode-map (kbd "C-c -") 'semantic-tag-folding-fold-block)
(define-key semantic-tag-folding-mode-map (kbd "C-c +") 'semantic-tag-folding-show-block)
(define-key semantic-tag-folding-mode-map (kbd "C-_") 'semantic-tag-folding-fold-all)
(define-key semantic-tag-folding-mode-map (kbd "C-+") 'semantic-tag-folding-show-all)

;; 打开semantic-tag-folding-mode后，用gdb调试时不能点左侧的fringe切换断点了，所以我把C-?定义为semantic-tag-folding-mode的切换键，在gdb调试时临时把semantic-tag-folding关掉：
;;(global-set-key (kbd "C-?") 'global-semantic-tag-folding-mode)

;;;; to avoid not alignment in terminal, use senator-fold-tag (default setting & key-binding, the same with key-binding ahead)
;;;; use semantic-tag-folding only under X
;; (when (and window-system (require 'semantic-tag-folding nil 'noerror))
;;   (global-semantic-tag-folding-mode 1)
;;   (global-set-key (kbd "C-?") 'global-semantic-tag-folding-mode)
;;   (define-key semantic-tag-folding-mode-map (kbd "C-c , -") 'semantic-tag-folding-fold-block)
;;   (define-key semantic-tag-folding-mode-map (kbd "C-c , +") 'semantic-tag-folding-show-block)
;;   (define-key semantic-tag-folding-mode-map (kbd "C-_") 'semantic-tag-folding-fold-all)
;;   (define-key semantic-tag-folding-mode-map (kbd "C-+") 'semantic-tag-folding-show-all))



;;;; others
;;(require 'semantic-ia)
;;(require 'semantic-gcc)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  end of CEDET  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  ECB(code browse)  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(add-to-list 'load-path "~/.emacs.d/ecb")
(require 'ecb)
(require 'ecb-autoloads)
(setq ecb-tip-of-the-day nil)

;;切换到目录窗口 Ctrl-c . g d
;;切换到函数/方法窗口 Ctrl-c . g m
;;切换到文件窗口 Ctrl-c . g s
;;切换到历史窗口 Ctrl-c . g h
;;切换到上一个编辑窗口 Ctrl-c . g l

;;;; other (get from emacser.com)
;; ;; ecb
;; (require 'ecb-autoloads nil 'noerror)
;; (unless (boundp 'stack-trace-on-error)
;;   (defvar stack-trace-on-error nil))
;; (when (fboundp 'ecb-minor-mode)
;;   (setq ecb-primary-secondary-mouse-buttons 'mouse-1--C-mouse-1
;;         ecb-source-path '("/")
;;         ecb-layout-name 'left3
;;         ecb-toggle-layout-sequence '("left3"
;;                                      "left8"
;;                                      "left-analyse"
;;                                      "left-symboldef")
;;         ;; ecb-windows-width 0.25
;;         ecb-compile-window-height 0.15
;;         ecb-compile-window-width 'edit-window
;;         ecb-compile-window-temporally-enlarge 'after-selection
;;         ;; ecb-enlarged-compilation-window-max-height 0.8
;;         ecb-tip-of-the-day nil
;;         ecb-auto-compatibility-check nil))
;; (eval-after-load "ecb"
;;   '(progn
;;      (setq ecb-compilation-buffer-names
;;            (append ecb-compilation-buffer-names '(("*Process List*")
;;                                                   ("*Proced*")
;;                                                   (".notes")
;;                                                   ("*appt-buf*")
;;                                                   ("*Compile-Log*")
;;                                                   ("*etags tmp*")
;;                                                   (" *svn-process*")
;;                                                   ("*svn-info-output*")
;;                                                   ("*Python Output*")
;;                                                   ("*Org Agenda*")
;;                                                   (" *EMMS Playlist*")
;;                                                   ("*Directory"))))
;;      (setq ecb-compilation-major-modes
;;            (append ecb-compilation-major-modes '(change-log-mode
;;                                                  calendar-mode
;;                                                  diary-mode
;;                                                  diary-fancy-display-mode
;;                                                  xgtags-select-mode
;;                                                  svn-status-mode
;;                                                  svn-info-mode
;;                                                  svn-status-diff-mode
;;                                                  svn-log-view-mode
;;                                                  svn-log-edit-mode
;;                                                  erc-mode
;;                                                  gud-mode)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  end of ECB  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;pulse ,http://emacser.com/pulse.htm 
(require 'pulse)
 
(defadvice exchange-point-and-mark-nomark (after pulse-advice activate)
  "Cause the line that is `goto'd to pulse when the cursor gets there."
  (when (and pulse-command-advice-flag (interactive-p)
		 (> (abs (- (point) (mark))) 400))
	(pulse-momentary-highlight-one-line (point))))
 
(defadvice switch-to-buffer (after pulse-advice activate)
  "Cause the current line of new buffer to pulse when the cursor gets there."
  (when (and pulse-command-advice-flag (interactive-p))
	(pulse-momentary-highlight-one-line (point))))
 
(defadvice ido-switch-buffer (after pulse-advice activate)
  "Cause the current line of new buffer to pulse when the cursor gets there."
  (when (and pulse-command-advice-flag (interactive-p))
	(pulse-momentary-highlight-one-line (point))))
 
(defadvice switch-to-other-buffer (after pulse-advice activate)
  "Cause the current line of new buffer to pulse when the cursor gets there."
  (when (and pulse-command-advice-flag (interactive-p))
	(pulse-momentary-highlight-one-line (point))))
 
(defadvice visit-.emacs (after pulse-advice activate)
  "Cause the current line of .emacs buffer to pulse when the cursor gets there."
  (when (and pulse-command-advice-flag (interactive-p))
	(pulse-momentary-highlight-one-line (point))))
 
(defadvice beginning-of-buffer (after pulse-advice activate)
  "Cause the current line of buffer to pulse when the cursor gets there."
  (when (and pulse-command-advice-flag (interactive-p))
	(pulse-momentary-highlight-one-line (point))))

;; (defadvice next-line (after pulse-advice activate)
;;   "Cause the current line of buffer to pulse when the cursor gets there."
;;   (when (and pulse-command-advice-flag (interactive-p))
;; 	(pulse-momentary-highlight-one-line (point))))

;; (defadvice previous-line (after pulse-advice activate)
;;   "Cause the current line of buffer to pulse when the cursor gets there."
;;   (when (and pulse-command-advice-flag (interactive-p))
;; 	(pulse-momentary-highlight-one-line (point))))

(defadvice goto-line (after pulse-advice activate)
  "Cause the current line of buffer to pulse when the cursor gets there."
  (when (and pulse-command-advice-flag (interactive-p))
	(pulse-momentary-highlight-one-line (point))))

(defadvice tabbar-forward (after pulse-advice activate)
  "Cause the current line of buffer to pulse when the cursor gets there."
  (when (and pulse-command-advice-flag (interactive-p))
	(pulse-momentary-highlight-one-line (point))))

(defadvice tabbar-backward (after pulse-advice activate)
  "Cause the current line of buffer to pulse when the cursor gets there."
  (when (and pulse-command-advice-flag (interactive-p))
	(pulse-momentary-highlight-one-line (point))))

(pulse-toggle-integration-advice (if window-system 1 -1))


;;;others
;;http://www.youtube.com/watch?v=EQAd41VAXWo&feature=related
;;


;;emacs编辑远程主机文件，和打开编辑一般文件没有什么特别，除了打开文件的方式有点特殊外，格式如下
;;/host:filename  
;;/user@host:filename  
;;/user@host#port:filename  
;;/method:user@host:filename  
;;/method:user@host#port:filename  
;;method可以是：ftp,ssh,rlogin,telnet等可以远程登录的程序，其缺省的是 
;;如果主机名称以"ftp."开始，那就用ftp
;;如果用户名称是ftp或者anonymous，那也用ftp
;;其余的缺省是ssh


;;other for artistic
;;智能的改变光标形状
;;http://emacser.com/cursor-change.htm
;;(require 'cursor-chg)  ; Load this library
;;(change-cursor-mode t) ; On for overwrite/read-only/input mode
;;(toggle-cursor-type-when-idle t) ; On when idle









;;自动补全功能，这是从王垠主页直接copy来的，引用一些他对此的说明。
;;你可以设置以下hippie-expand的补全方式。它是一个优先列表，
;;hippie-expand会优先使用表最前面的函数来补全。
;;这是说，首先使用当前的buffer补全，如果找不到，就到别的可见的窗口里寻找，
;;如果还找不到，那么到所有打开的buffer去找，如果还……那么到kill-ring里，
;;到文件名，到简称列表里，到list......当前使用的匹配方式会在echo区域显示。
;;特别有意思的是try-expand-line，它可以帮你补全整整一行文字。我很多时候有
;;两行文字大致相同，只有几个字不一样，但是我懒得去copy paste一下。那么我就
;;输入这行文字的前面几个字。然后多按几下M-/就能得到那一行。
;; (global-set-key [(meta ?/)] 'hippie-expand)
;; (setq hippie-expand-try-functions-list
;; '(try-expand-line
;;   try-expand-line-all-buffers
;;   try-expand-list
;;   try-expand-list-all-buffers
;;   try-expand-dabbrev
;;   try-expand-dabbrev-visible
;;   try-expand-dabbrev-all-buffers
;;   try-expand-dabbrev-from-kill
;;   try-complete-file-name
;;   try-complete-file-name-partially
;;   try-complete-lisp-symbol
;;   try-complete-lisp-symbol-partially
;;   try-expand-whole-kill))








;;;;;;;;;;;;;;;;;;;;
;;; 自动补全功能 ;;;
;;;;;;;;;;;;;;;;;;;;
;; (global-set-key (kbd "M-/") 'hippie-expand) ;; 绑定该功能到M-/
;; (setq hippie-expand-try-functions-list
;;  '(try-complete-file-name-partially         ;; 文件名部分匹配
;; 	 try-complete-file-name             ;; 文件名匹配
;; 	 try-expand-all-abbrevs             ;; 匹配所有缩写词
;; 	 try-expand-list                    ;; 补全一个列表
;; 	 try-expand-line                    ;; 补全当前行
;; 	 try-expand-dabbrev                 ;; 搜索当前buffer
;; 	 try-expand-dabbrev-all-buffers     ;; 搜索所有buffer
;; 	 try-expand-dabbrev-from-kill       ;; 从kill-ring中搜索
;; 	 try-complete-lisp-symbol-partially ;; 部分补全lisp symbol
;; 	 try-complete-lisp-symbol))         ;; 补全lisp symbol



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; below ?????????????????????????????????
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;




;;;;;;;;;;programming
;shell,gdb退出后，自动关闭该buffer
(add-hook 'shell-mode-hook 'mode-hook-func)
(add-hook 'gdb-mode-hook 'mode-hook-func)
(defun mode-hook-func ()
(set-process-sentinel (get-buffer-process (current-buffer))
#'kill-buffer-on-exit)
)
(defun kill-buffer-on-exit (process state)
(message "%s" state)
(if (or
(string-match "exited abnormally with code.*" state)
(string-match "finished" state))
(kill-buffer (current-buffer)))) 
;; ;;多窗口gdb
;; (add-to-list 'load-path
;; "~/.emacs.d/plugins")
;; (setq gdb-many-windows t)
;; ;;(load-library "multi-gud.el")
;; ;;(load-library "multi-gdb-ui.el")


;;settings for c language
(global-set-key (kbd "C-x /") 'indent-for-comment)
(global-set-key (kbd "M-o") 'ff-find-other-file)


;;other default keybind
;;C-c C-q : runs the command c-indent-defun
;;







;;refer:
;;http://code.google.com/p/dea/  ;;Dot emacs of ahei


;;;; code below is auto generated by CEDET

(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(ecb-options-version "2.40"))
(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 )
